<template>
  <div class="container">
    <div class="app-container">
      <!-- 添加角色按钮 -->
      <div style="padding:10px">
        <el-button type="primary" size="mini" @click="showDialog = true">添加角色</el-button>
      </div>
      <!-- 角色列表 -->
      <el-table
        :data="roleList"
        style="width: 100%"
      >
        <el-table-column
          prop="name"
          label="角色"
          width="200"
          align="center"
        >
          <template #default="{row}">
            <!-- <input v-if=" row.isEdit" type="text"> -->
            <el-input v-if="row.isEdit" v-model="row.editRow.name" size="mini" />
            <span v-else>{{ row.name }} </span>
          </template>
        </el-table-column>
        <el-table-column
          prop="state"
          label="启用"
          width="200"
          align="center"
        >
          <template #default="{row}">
            <el-switch v-if="row.isEdit" v-model="row.editRow.state" :active-value="1" :inactive-value="0" />
            <span v-else>{{ row.state ===0 ? '未启用' :'已启用' }} </span>
          </template>
        </el-table-column>
        <el-table-column
          prop="description"
          label="描述"
          align="center"
        >
          <template #default="{row}">
            <el-input v-if="row.isEdit" v-model="row.editRow.description" type="textarea" size="mini" />
            <span v-else>{{ row.description }} </span>
          </template>
        </el-table-column>
        <el-table-column
          prop="address"
          label="操作"
          align="center"
        >
          <template #default="{row}">
            <div v-if="row.isEdit">
              <el-button type="primary" size="mini" @click="btnClick(row)">确定 </el-button>
              <el-button size="mini" @click="row.isEdit = false">取消 </el-button>
            </div>

            <div v-else>
              <el-button type="text" size="mini">分配权限</el-button>
              <el-button type="text" size="mini" style="padding:10px" @click="editClick(row)">编辑</el-button>
              <el-popconfirm
                title="这是一段内容确定删除吗？"
                @onConfirm="delClock(row)"
              >
                <el-button slot="reference" type="text" size="mini">删除</el-button>
              </el-popconfirm>
            </div>
          </template>

        </el-table-column>

      </el-table>
      <el-row type="flex" align="middle" justify="end" style="height:60px">
        <el-pagination
          layout="prev, pager, next"
          :current-page="pageParams.page"
          :page-size="pageParams.pagesize"
          :total="pageParams.total"
          @current-change="currentChange"
        />
      </el-row>

    </div>
    <!-- 弹层 -->
    <el-dialog
      title="新增角色"
      :visible="showDialog"
      width="500px"
      @close="handleClose"
    >
      <el-form ref="roleForm" :model="roleForm" :rules="roleRules" label-width="80px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" size="mini" style="width:300px" />
        </el-form-item>

        <el-form-item label="角色状态" prop="state">
          <el-switch v-model="roleForm.state" size="mini" :active-value="1" :inactive-value="0" />
        </el-form-item>

        <el-form-item label="角色描述" prop="description">
          <el-input v-model="roleForm.description" type="textarea" :rows="3" size="mini" style="width:300px" />
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="center" style="width:300px">
            <el-button type="primary" size="mini" @click="confirmClick">确认</el-button>
            <el-button size="mini" @click="handleClose">取消</el-button>
          </el-row>

        </el-form-item>
      </el-form>
    </el-dialog>

  </div>
</template>
<script>
import { addRoleApi, delApi, getRoleApi, updateRole } from '@/api/role'
export default {
  name: 'Role',
  data() {
    return {
      // 角色列表数据
      roleList: [],
      // 分页查询参数
      pageParams: {
        page: 1, // 显示当前页
        pagesize: 5, // 每页显示条数
        total: 0 // 总条数
      },
      // 控制弹窗显示和隐藏
      showDialog: false,
      // 角色表单数据对象
      roleForm: {
        name: '', // 角色名称
        description: '', // 角色描述
        state: 1 // 角色状态
      },
      roleRules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入角色描述', trigger: 'blur' }

        ]
      }
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    // 调用角色列表接口
    async getRoleList() {
      const { rows, total } = await getRoleApi(this.pageParams)
      this.roleList = rows
      this.pageParams.total = total
      this.roleList.forEach(item => {
        // 经验:利用JS的语法动态添加的属性，都是非响应式的，数据即使变化了，也不会影响视图
        // item.isEdit = false
        // 解决: 利用Vue的语法，动态的给对象设置响应式的属性
        // this.$set(目标对象, '属性', 初始值) vue的方法往数组对象中添加响应式数据
        this.$set(item, 'isEdit', false)
        this.$set(item, 'editRow', {
          name: item.name,
          description: item.description,
          state: item.state
        })
      })
    },
    currentChange(currentPage) {
      this.pageParams.page = currentPage

      this.getRoleList()
    },
    handleClose() {
      this.showDialog = false
      this.$refs.roleForm.resetFields()
    },
    // 点击弹层的确定按钮
    confirmClick() {
      // 兜底校验
      this.$refs.roleForm.validate(async valid => {
        if (!valid) return
        // 调用新增角色的接口
        await addRoleApi(this.roleForm)
        // 提示用户
        this.$message.success('新增角色成功')
        // 重新渲染页面
        this.getRoleList()
        // 重置表单,关闭弹窗
        this.handleClose()
      })
    },
    // 点击了编辑按钮
    editClick(row) {
      row.isEdit = true
      row.editRow.name = row.name
      row.editRow.state = row.state
      row.editRow.description = row.description
    },
    // 点击了编辑后的确定按钮
    async btnClick(row) {
      if (!row.editRow.naem && !row.editRow.description) this.$message.warning('名称和描述不能为空')
      await updateRole({ ...row.editRow, id: row.id })
      // 提示用户
      this.$message.success('修改成功')
      Object.assign(row, {
        isEdit: false,
        ...row.editRow
      })
    },
    // 删除
    async delClock(row) {
      await delApi(row.id)
      this.$message.success('删除角色成功')
      if (this.pageParams.page > 1 && this.roleList.length === 1) {
        this.pageParams.page--
      }
      this.getRoleList()
    }
  }
}
</script>
